<?php

namespace Admin\Controller;

use Think\Controller;

class AdminController extends Controller
{
    // 获取模块及模块下面的菜单
    public function index()
    {
        if(IS_POST) {
            /*$_data = M('system')
                ->join('menu on system.id=menu.pid', 'left')
                ->where('')
                ->order('system.sort', 'desc')
                ->select();
            $this->get_system($_data);*/
            if($_POST['path'] != '/') {
                $pid = M('menu')->where("'{$_POST['path']}' = url")->getField('pid');
                $m_name = M('menu')->where("'{$_POST['path']}' = url")->getField('m_name');
            }
            $_system = M('system')->where('')->order('sort', 'desc')->select();
            if(empty($_system)) return false;
            $_data = array();
            foreach($_system as $k=>$v) {
                $_menu = M('menu')->where("{$v['id']} = pid")->order('sort', 'desc')->select();
                // if(empty($_menu)) return false;
                $v['menu'] = $_menu;
                $_data[] = $v;
            }
            $_data['pid'] = isset($pid) ? $pid : '';
            $_data['m_name'] = isset($m_name) ? $m_name : '';
            $this->ajaxReturn($_data,'json');
        }
    }

    public function get_system($data)
    {
        $arr = array();
        foreach($data as $k=>$v){
            if($v['id']==0){
                $v['sub'] = $this->get_system($data);
                $arr[] = $v;
            }
        }
        return $arr;
    }

}